package cn.dhnep.demo.service.impl;

import cn.dhnep.demo.mapper.AdminMapper;
import cn.dhnep.demo.mapper.AdminRoleMapper;
import cn.dhnep.demo.mapper.RoleMapper;
import cn.dhnep.demo.pojo.Admin;
import cn.dhnep.demo.pojo.AdminRole;
import cn.dhnep.demo.pojo.LoginRecord;
import cn.dhnep.demo.pojo.vo.RespPageBean;
import cn.dhnep.demo.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dhn
 * @since 2021-08-09
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Resource
    private AdminRoleMapper adminRoleMapper;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public Admin userlogin(String name, String password) {
        return baseMapper.userlogin(name, password);
    }

    @Override
    public RespPageBean getAllAdmin(Integer currentPage, Integer size, String adminName, String adminPhone, String adminNickname) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(adminName)) wrapper.like("admin_name", adminName);
        if (!StringUtils.isEmpty(adminPhone)) wrapper.like("admin_phone", adminPhone);
        if (!StringUtils.isEmpty(adminNickname)) wrapper.like("admin_nickName", adminNickname);

        //开始分页
        Page<Admin> page = new Page<>(currentPage, size);
        baseMapper.selectPage(page, wrapper);
        List<Admin> adminList = page.getRecords();

        List<AdminRole> adminRoles = adminRoleMapper.selectList(null); //关系表
        for (Admin admin : adminList) {
            admin.setAdminPassword(null);
            List<Integer> roleIds = adminRoles.stream().filter(adminRole -> adminRole.getAdminId().equals(admin.getAdminId()))
                    .map(AdminRole::getRoleId).collect(Collectors.toList());
            if (!roleIds.isEmpty()) {
                //设置角色
                admin.setRoles(roleMapper.selectBatchIds(roleIds));
            }
        }

        return new RespPageBean(0, page.getTotal(), adminList);
    }

    @Transactional
    @Override
    public boolean addAdminRole(Admin admin) {
        if (baseMapper.insert(admin) > 1) {
            //设置用户与角色的关系表
            AdminRole adminRole = new AdminRole().setAdminId(admin.getAdminId());
            for (Integer roleId : admin.getRoleIds()) {
                adminRoleMapper.insert(adminRole.setRoleId(roleId));
            }
            return true;
        }
        return false;
    }

    @Override
    public int addloginrecord(LoginRecord lr) {
        return baseMapper.addloginrecord(lr);
    }

    @Override
    public List<LoginRecord> selectPageloginrecordList(cn.dhnep.demo.util.Page page) {
        return baseMapper.selectPageloginrecordList(page);
    }

    @Override
    public Integer selectPageloginrecordCount(cn.dhnep.demo.util.Page page) {
        return baseMapper.selectPageloginrecordCount(page);
    }
}
